package com.shushan.pages;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.android.xrecyclerview.ProgressStyle;
import com.android.xrecyclerview.XRecyclerView;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetListCommListener;
import com.shushan.pages.bean.ExerciseRecordListDetailsBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.DateUtils;
import com.shushan.util.ToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ExerciseRecordDetailsActivity
 * @Desciption: //练习记录详情页
 * @author: zhangshihao
 * @date: 2018-04-11
 */
public class ExerciseRecordDetailsActivity extends NavigationLoseActivity implements XRecyclerView.LoadingListener {

    private static final String TAG = ExerciseRecordDetailsActivity.class.getSimpleName();

    private XRecyclerView mListView;
    private ExerciseRecordDetailsAdapter adapter;

    private String practice_type;

    private int page = 1;
    private final static int PAGE_SIZE = 20;
    private List<ExerciseRecordListDetailsBean> exerciseRecordList = new ArrayList<ExerciseRecordListDetailsBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_record_details);
        Intent intent = getIntent();
        practice_type = intent.getStringExtra("practice_type");
        setTitle(ToolUtils.getExerciseTypeNameByTypeId(Integer.parseInt(practice_type))+"记录");

        initView();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mListView != null){
            mListView.destroy();
            mListView = null;
        }
    }

    private void initView(){
        mListView = findViewById(R.id.lv_exercise_rocord_details);
        mListView.setPullRefreshEnabled(false);
        mListView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        Drawable dividerDrawable = ContextCompat.getDrawable(this, R.drawable.exercise_divider_shape);
        mListView.addItemDecoration(mListView.new DividerItemDecoration(dividerDrawable));
        mListView.getDefaultFootView().setLoadingHint("正在加载中...");
        mListView.getDefaultFootView().setNoMoreHint("到底了");
        mListView.setLoadingListener(this);

        adapter = new ExerciseRecordDetailsAdapter(exerciseRecordList);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int paper_id,String create_time) {
                Intent intent = new Intent(ExerciseRecordDetailsActivity.this,NewRecordResultActivity.class);
                intent.putExtra("paper_id",paper_id);
                intent.putExtra("create_time",create_time);
                intent.putExtra("practice_type",String.valueOf(practice_type));
                startActivityAllow(intent);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mListView.setLayoutManager(layoutManager);
        mListView.setAdapter(adapter);
    }

    private void getData(){
        int child_id = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.EXERCISE_RECORD_LIST_DETAILS_NEW);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id",child_id)
                .putValue("page",page)
                .putValue("pagesize",PAGE_SIZE)
                .putValue("practice_type",practice_type);
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetListCommListener<ExerciseRecordListDetailsBean>() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(List<ExerciseRecordListDetailsBean> exerciseRecordListDetailsBeans) {
                        if(exerciseRecordListDetailsBeans.size() < PAGE_SIZE){
                            mListView.setNoMore(true);
                            exerciseRecordList.addAll(exerciseRecordListDetailsBeans);
                            adapter.notifyDataSetChanged();
                            mListView.getFootView().setVisibility(View.GONE);
                            return;
                        }

                        exerciseRecordList.addAll(exerciseRecordListDetailsBeans);
                        adapter.notifyDataSetChanged();
                        mListView.loadMoreComplete();
                        page++;

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        if(netRetBean.getServerCode() == 400){
                            mListView.setNoMore(true);
                            adapter.notifyDataSetChanged();
                            mListView.getFootView().setVisibility(View.GONE);
                        }else {
                            showNetError(netRetBean);
                        }

                    }
                });
    }

    private class ExerciseRecordDetailsAdapter extends RecyclerView.Adapter<ExerciseRecordDetailsAdapter.ViewHolder>{

        private List<ExerciseRecordListDetailsBean> exerciseRecordListDetailsBeans;
        private OnItemClickListener listener;

        class ViewHolder extends RecyclerView.ViewHolder{
            TextView tvDate;
            TextView tvTime;
            TextView tvExerciseCount;
            TextView tvRightCount;
            TextView tvScore;

            public ViewHolder(View itemView) {
                super(itemView);

                tvDate = itemView.findViewById(R.id.tv_date);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvExerciseCount = itemView.findViewById(R.id.tv_exercise_count);
                tvRightCount = itemView.findViewById(R.id.tv_right_count);
                tvScore = itemView.findViewById(R.id.tv_score);
            }
        }

        public void setOnItemClickListener(OnItemClickListener listener){
            this.listener = listener;
        }

        public ExerciseRecordDetailsAdapter(List<ExerciseRecordListDetailsBean> exerciseRecordListDetailsBeans){
            this.exerciseRecordListDetailsBeans = exerciseRecordListDetailsBeans;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(ExerciseRecordDetailsActivity.this).inflate(R.layout.item_lv_exercise_record_details,parent,false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

            if(exerciseRecordListDetailsBeans == null){
                return;
            }

            ExerciseRecordListDetailsBean bean = exerciseRecordListDetailsBeans.get(position);
            holder.tvDate.setText(DateUtils.captureDateByFormat(bean.create_time,"yyyy-MM-dd"));
            holder.tvTime.setText(DateUtils.captureTimeWithoutSecond(bean.create_time));
            holder.tvExerciseCount.setText(String.valueOf(bean.exercises_count));
            holder.tvRightCount.setText(String.valueOf(bean.right_count));
            holder.tvScore.setText(String.valueOf(bean.score/100));
            final int paper_id = bean.paper_id;
            final String create_time = bean.create_time;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        listener.onItemClick(paper_id,create_time);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return exerciseRecordListDetailsBeans.size();
        }

    }

    public interface OnItemClickListener{
        void onItemClick(int paper_id, String create_time);
    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        getData();
    }
}
